package cn.formaggie.service.impl;

import cn.formaggie.dao.WorkerDao;
import cn.formaggie.entity.model.roleandpermission.Worker;
import cn.formaggie.service.face.WorkerService;
import cn.formaggie.util.CryptographyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("workerService")
public class WorkerServiceImpl implements WorkerService {


    @Autowired
    private WorkerDao workerDao;

    @Override
    public Worker getSimpleWorkerByWorkerId(String workerId) {
        return workerDao.getSimpleWorkerByWorkerId(workerId);
    }

    @Override
    public Worker getWorkerByWorkerId(String workerId) {
        return workerDao.getWorkerByWorkerId(workerId);
    }

    @Override
    public Boolean modifyWorkerInfo(Worker worker, HttpSession session) {
       Worker currenter= (Worker) session.getAttribute("worker");
       if(worker.getWorkerId().equals(currenter.getWorkerId())) {
           workerDao.modifyWorkerInfo(worker);
           return true;
       }else{
           return false;
       }
    }

    @Override
    public Boolean modifyWorkerInfo(Worker worker) {
        return workerDao.modifyWorkerInfo(worker)>0?true:false;
    }

    @Override
    public Boolean modifyWorkerPsw(String workerId, String password) {
        Map<String,String> map=new HashMap<>();
        map.put("workerId",workerId);
        map.put("password",password);
        return workerDao.modifyWorkerPsw(map)>0?true:false;
    }

    @Override
    public List<Worker> findWorkers(Map<String, Object> map) {
        return workerDao.findWorkers(map);
    }

    @Override
    public Integer getWorkersCount(Map<String, Object> map) {
        return workerDao.getWorkersCount(map);
    }

    @Override
    public Boolean addWorker(Worker worker) {
        if(workerDao.getWorkerByWorkerId(worker.getWorkerId())!=null){
            return false;
        }
        worker.setPassword(CryptographyUtil.MD5("123456",worker.getWorkerId()));
        return workerDao.addWorker(worker)>0?true:false;
    }

    @Override
    public Boolean deleteWorkerByWorkerId(Integer id) {
        return workerDao.deleteWorkerByWorkerId(id)>0?true:false;
    }
}
